using System;
using RockHopper.Common.Model;
using Windows.UI.Xaml.Controls;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class NewRacePredictionControl : Control
    {
        private ComboBox distanceCombo;
        public event EventHandler AddClick;

        public NewRacePredictionControl()
        {
            DefaultStyleKey = typeof(NewRacePredictionControl);
        }

        public RaceDistance RaceDistance { get; private set; }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            distanceCombo = (ComboBox) GetTemplateChild("DistanceComboPart");
            distanceCombo.SelectionChanged += OnDistanceComboSelectionChanged;
            distanceCombo.Items.Add(new RaceDistance(800, UnitType.Metres));
            distanceCombo.Items.Add(new RaceDistance(1000, UnitType.Metres));
            distanceCombo.Items.Add(new RaceDistance(1, UnitType.Kilometres));
            distanceCombo.Items.Add(new RaceDistance(1500, UnitType.Metres));
            distanceCombo.Items.Add(new RaceDistance(1, UnitType.Miles));
            distanceCombo.Items.Add(new RaceDistance(2, UnitType.Miles));
            distanceCombo.Items.Add(new RaceDistance(3, UnitType.Miles));
            distanceCombo.Items.Add(new RaceDistance(5000, UnitType.Metres));
            distanceCombo.Items.Add(new RaceDistance(5, UnitType.Kilometres));
            distanceCombo.Items.Add(new RaceDistance(5, UnitType.Miles));
            distanceCombo.Items.Add(new RaceDistance(10000, UnitType.Metres));
            distanceCombo.Items.Add(new RaceDistance(10, UnitType.Kilometres));
            distanceCombo.Items.Add(new RaceDistance(15, UnitType.Kilometres));
            distanceCombo.Items.Add(new RaceDistance(10, UnitType.Miles));
            distanceCombo.Items.Add(new RaceDistance(20, UnitType.Kilometres));
            distanceCombo.Items.Add(new RaceDistance(21.0975, UnitType.Kilometres, "Half Marathon"));
            distanceCombo.Items.Add(new RaceDistance(42.195, UnitType.Kilometres, "Marathon"));
        }

        private void OnDistanceComboSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                RaceDistance = (RaceDistance)e.AddedItems[0];

                if (AddClick != null)
                    AddClick(this, EventArgs.Empty);

                distanceCombo.SelectedValue = null;
            }
        }
    }
}